package com.jiyun.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.om.entity.PickupOrder;
import com.jiyun.om.entity.ReceiptConfirmation;
import com.jiyun.om.entity.Waybill;
import com.jiyun.result.Result;
import com.jiyun.service.IReceiptConfirmationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 回单表 前端控制器
 * </p>
 *
 * @author zhx
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/receipt-confirmation")
@Tag(name = "回单管理")
public class ReceiptConfirmationController {
@Autowired
    private IReceiptConfirmationService receiptConfirmationService;
@Operation(summary = "分页展示回单管理")
    @RequestMapping("/getAll")
    public Page<ReceiptConfirmation> getAll(Integer pageNum, Integer pageSize, @RequestBody ReceiptConfirmation receiptConfirmation){
        return receiptConfirmationService.getAll(pageNum,pageSize,receiptConfirmation);
    }
    @Operation(summary = "上传回单")
    @RequestMapping("/toHuiDan")
    public Result toHuiDan(@RequestBody ReceiptConfirmation receiptConfirmation){
        receiptConfirmationService.save(receiptConfirmation);
        return Result.ok();
    }
    @Operation(summary = "导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<ReceiptConfirmation> list = receiptConfirmationService.findAll();
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("订单号");
        row.createCell(1).setCellValue("客户");
        row.createCell(2).setCellValue("回单状态");
        row.createCell(3).setCellValue("订单状态");
        row.createCell(4).setCellValue("回单份数");
        row.createCell(5).setCellValue("发货地址");
        row.createCell(6).setCellValue("发货时间");
        row.createCell(7).setCellValue("收货地址");
        row.createCell(8).setCellValue("签收数量");
        row.createCell(9).setCellValue("签收人");
        row.createCell(10).setCellValue("签收时间");
        for (int i = 0; i < list.size(); i++) {
            ReceiptConfirmation receiptConfirmation = list.get(i);
            XSSFRow xssfRow = sheet.createRow(i + 1);
            xssfRow.createCell(0).setCellValue(receiptConfirmation.getOrderId());
            xssfRow.createCell(1).setCellValue(receiptConfirmation.getCustomer());
            xssfRow.createCell(2).setCellValue(receiptConfirmation.getConfirmationStatus());
            xssfRow.createCell(3).setCellValue(receiptConfirmation.getOrderStatus());
            xssfRow.createCell(4).setCellValue(receiptConfirmation.getConfirmationCount());
            xssfRow.createCell(5).setCellValue(receiptConfirmation.getShippingAddress());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(receiptConfirmation.getSenderTime());
            xssfRow.createCell(6).setCellValue(format);
            xssfRow.createCell(7).setCellValue(receiptConfirmation.getReceivingAddress());
            xssfRow.createCell(8).setCellValue(receiptConfirmation.getTotalQuantity());
            xssfRow.createCell(9).setCellValue(receiptConfirmation.getSignerName());
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format1 = simpleDateFormat1.format(receiptConfirmation.getReceiptTime());
            xssfRow.createCell(10).setCellValue(format1);
        }
        response.setHeader("Content-Disposition", "attachment;filename=ReceiptConfirmation.xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();

    }
}
